package com.ithinks.flow.common.utils.sms.ali;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ithinks.flow.common.utils.sms.SmsUtil;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: SendSmsUtils1
 * @Date: 2020/11/24 14:56
 * @Description: 阿里短信发送工具类
 */
@Component
public class AliSmsUtil {
    /**
     * 产品域名,开发者无需替换
     */
    static final String DOMAIN = "dysmsapi.aliyuncs.com";

    /**
     * 单发系统规定参数。取值：SendSms。
     */
    static final String ACTION = "SendSms";

    /**
     * 群发系统规定参数。取值：SendBatchSms。
     */
    static final String SENDBATCHSMS = "SendBatchSms";

    /**
     * 验证码短信模板,单发
     *
     * @param accessKeyId  必填：自己的ak
     * @param accessKeySecret  必填:自己的aks
     * @param telephone 必填:接收人电话
     * @param code 必填:短信模板code
     * @param smsName 必填:短信签名
     * @param smsUpExtendCode 选填:上行短信扩展码(用来查看回复的短信)，无特殊需要此字段的用户请忽略此字段。
     * @param outId  选填:外部流水扩展字段。可忽略
     * @return CommonResponse
     * @throws ClientException
     */
    public static CommonResponse sendSms(String accessKeyId, String accessKeySecret, String telephone,
                                         String code, String smsName, String smsUpExtendCode,
                                         String outId) throws ClientException {

        // 可自助调整超时时间,后面是毫秒值,如果不设置,如果网络异常,可能会长时间卡在这里
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion("2017-05-25");
        request.setSysAction(ACTION);
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", smsName);
        request.putQueryParameter("TemplateCode", code);
        request.putQueryParameter("TemplateParam", SmsUtil.randomCode());
        request.putQueryParameter("SmsUpExtendCode", smsUpExtendCode);
        request.putQueryParameter("OutId", outId);
        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.getData());
        return response;
    }

    /**
     * 验证码短信模板,群发
     *
     * @param accessKeyId  必填：自己的ak
     * @param accessKeySecret  必填:自己的aks
     * @param telephone 必填:接收人电话
     * @param code 必填:短信模板code
     * @param smsName 必填:短信签名
     * @param smsUpExtendCode 选填:上行短信扩展码(用来查看回复的短信)，无特殊需要此字段的用户请忽略此字段。
     * @param outId  选填:外部流水扩展字段。可忽略
     * @return CommonResponse
     * @throws ClientException
     */
    public static CommonResponse sendSmsMany(String accessKeyId, String accessKeySecret, List<String> telephone,
                                             Map<String, String> templateParam, String code, List<String> smsName , String smsUpExtendCode,
                                             String outId) throws ClientException {

        // 可自助调整超时时间,后面是毫秒值,如果不设置,如果网络异常,可能会长时间卡在这里
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion("2017-05-25");
        request.setSysAction(SENDBATCHSMS);
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumberJson",JSONArray.fromObject(telephone).toString());
        request.putQueryParameter("SignNameJson", JSONArray.fromObject(smsName).toString());
        request.putQueryParameter("TemplateCode", code);
        request.putQueryParameter("TemplateParamJson", SmsUtil.toJsonStr(templateParam));
        request.putQueryParameter("SmsUpExtendCode", smsUpExtendCode);
        request.putQueryParameter("OutId", outId);
        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.getData());
        return response;
    }
}
